package cn.com.db.service;

import cn.com.db.model.Cart;
import cn.com.db.model.DiyCart;
import cn.com.db.model.User;
import cn.com.util.TimeUtil;
import cn.com.util.UidUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;

@Service
public class UserService extends BaseService{

    @Transactional
    public void createUser(User user){
        userDao.createUser(user);
//        用户注册的同时初始化购物车
        Cart cart=cartDao.getUserCart(user.getId());
        if (cart==null){
            cart=new Cart();
            cart.setId(UidUtil.createUUID());
            cart.setUser(user.getAuthing_id());
            cart.setCreated_at(TimeUtil.getNowTimestamp());
            cartDao.createCart(cart);
        }
        DiyCart diyCart=diyCartDao.getUserDiyCart(user.getId());
        if (diyCart==null){
            diyCart=new DiyCart();
            diyCart.setId(UidUtil.createUUID());
            diyCart.setUser(user.getAuthing_id());
            diyCart.setCreated_at(TimeUtil.getNowTimestamp());
            diyCartDao.createDiyCart(diyCart);
        }
    }

    public Integer getUserNum(){
        return userDao.getUserNum();
    }

    public User getUserAuthingId(String authingId){
        return userDao.getUserByAuthingId(authingId);
    }

    public User getUserById(String uid){
        return userDao.getUserById(uid);
    }

    public User getUserByOpenId(String id){
        return userDao.getUserByOpenId(id);
    };

    public void update(User user){
    	userDao.update(user);
    }

    public void addOpenid(User user){
    	userDao.addOpenid(user);
    }
}
